package com.bwie.filter;

import com.bwie.CreateMyException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class MyGloBalFilter implements Ordered, GlobalFilter {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public static final String code = "123456";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //白名单校验
//        if (whiteList(request)) {
//            return chain.filter(exchange);
//        }
//        String token = request.getHeaders().getFirst("token");
//        if (token == null) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        Claims body = null;
//        try {
//            body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
//        } catch (Exception e) {
//            throw new CreateMyException(401,"token过期,请重新登录");
//        }
//        String userId = body.get("userId").toString();
//        Date expiration = body.getExpiration();
        //获取设定的过期时间
//        long time = expiration.getTime();
//        long l = System.currentTimeMillis();
        //刷新token
//        if (l - time <= 300000L) {
//            String tokenLO = stringRedisTemplate.opsForValue().get(token);
//            stringRedisTemplate.delete(token);
//            String tokenLI = Jwts.builder().signWith(SignatureAlgorithm.HS256, code)
//                    .claim("userId", userId)
//                    .setExpiration(new Date(l + 1800000L))
//                    .compact();
//            stringRedisTemplate.opsForValue().set(tokenLI, tokenLO);
            response.getHeaders().add("X-New-Token", "测试token刷新回显");
//        }
        return chain.filter(exchange);
    }

    private boolean whiteList(ServerHttpRequest request) {
        String s = request.getPath().toString();
        List<String> strings = Arrays.asList("login");
        for (String string : strings) {
            if (s.contains(string)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
